﻿using Aplicacion.Examenes.Examenes;
using ExcellentProjectPlus.Dominio.Examenes.Entidades;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using System.Web.Http.Description;

namespace Servicios.Examenes.Controllers
{
    public class ExamenesController : ApiController
    {
        #region Propiedades
        IServicioDeGestionDeExamenes _servicioDeGestionDeExamenes;
        #endregion

        #region Constructores
        public ExamenesController(IServicioDeGestionDeExamenes servicioDeGestionDeExamenes)
        {
            _servicioDeGestionDeExamenes = servicioDeGestionDeExamenes;
        }
        #endregion

        //GET
        [HttpGet]
        [ResponseType(typeof(Examen))]
        public IHttpActionResult ObtenerTodos()
        {
            IEnumerable<Examen> examenes = _servicioDeGestionDeExamenes.ObtenerExamenes();
            if (examenes == null)
            {
                return NotFound();
            }

            return Ok(examenes);
        }


        //GET
        [HttpGet]
        [Route("api/examenes/{id}")]
        [ResponseType(typeof(Examen))]
        public IHttpActionResult ObtenerExamenPorId(int id)
        {
            Examen examen = _servicioDeGestionDeExamenes.ObtenerExamenPorId(id);
            if (examen == null)
            {
                return NotFound();
            }

            return Ok(examen);
        }

        [HttpPost]
        [ResponseType(typeof(Examen))]
        public IHttpActionResult PostExamen(Examen examen)
        {
            if (!ModelState.IsValid)
                return BadRequest(ModelState);

            _servicioDeGestionDeExamenes.AgregarExamen(examen);

            return CreatedAtRoute("DefaultApi", new { id = examen.IdExamen }, examen);
        }

        [HttpPut]
        [Route("api/examenes/{id}")]
        [ResponseType(typeof(Examen))]
        public IHttpActionResult PutExamen(int id, [FromBody]Examen examen)
        {
            if (!ModelState.IsValid)
                return BadRequest(ModelState);

            if (id != examen.IdExamen)
                return BadRequest();

            _servicioDeGestionDeExamenes.ModificarExamen(examen);

            return StatusCode(HttpStatusCode.NoContent);

        }

        [HttpDelete]
        [Route("api/examenes/{id}")]
        [ResponseType(typeof(Examen))]
        public IHttpActionResult DeleteExamen(int id)
        {
            Examen examen = _servicioDeGestionDeExamenes.ObtenerExamenPorId(id);

            if (examen == null)
                return NotFound();

            _servicioDeGestionDeExamenes.EliminarExamen(examen);
            return Ok(examen);
        }
    }
}